<?php

/**
 *  liuchaoliang 2015-02-6
 *  公用模型
 *
 */
class Comm_model extends CI_Model
{

    public function __construct()
    {
        parent::__construct();
    }


    /**
     * @param $page int 分页开始数
     * @param $offset  int 分页数量
     * @param $table string 查询数据表
     * @param $order_by string 排序方式字符串
     * @return array
     */
    public function get_list($page, $offset, $table, $order_by='')
    {
        $this->db->select('*');
        $this->db->from($table);
        $this->db->limit($offset, $page);
        if(!empty($order_by)){
            $this->db->order_by($order_by);
        }

        $query = $this->db->get();

        return $query->result_array();

    }

    /**
     * 获取总量
     * @return array
     */
    public function total_rows($table)
    {
        $this->db->select('count(*) as num');
        $this->db->from($table);

        $query = $this->db->get();
        $row = $query->row_array();
        return $row['num'];
    }

    /**
     * 获取一个信息
     * @param int $id
     * @return array
     */
    public function get_one($id, $table)
    {
        $this->db->select('*');
        $this->db->from($table);
        $this->db->where('id', $id);

        $query = $this->db->get();

        return $query->row_array();
    }

    /**
     *  修改
     */
    public function update($id, $arr, $table)
    {
        if (is_array($arr) && !empty($arr)) {
            return $this->db->update($table, $arr, array('id' => $id));
        } else {
            return false;
        }
    }

    /**
     *  添加
     */
    public function insert($table, $data)
    {
        $rs = $this->db->insert($table, $data);
        if (!empty($rs)) {
            return $this->db->insert_id();
        } else {
            return false;
        }
    }

    public function del($id, $table)
    {
        return $this->db->delete($table, array('id' => $id));
    }

    /**
     *  获取已有游戏城市
     */
    public function has_game_city()
    {

        $this->db->select('*');
        $this->db->from('area');
        $this->db->where('prize_sum >', 0);
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     *  获取所有城市
     */
    public function city_list()
    {
        $sql = "SELECT * 
                FROM  17koko_area 
                WHERE parent_id IN ( SELECT id FROM  17koko_area WHERE parent_id =1 )";
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    /**
     * 获取cityid
     */
    public function city_one($title)
    {
        $this->db->select('*');

        $this->db->from('area');

        $this->db->where('prize_sum >', 0);
        $this->db->where('area_name', $title);

        $query = $this->db->get();

        return $query->row_array();
    }
}

?>